//
//  NSAttributedString+Ext.swift
//  TTCOM
//
//  Created by Toj on 9/19/22.
//

import Foundation

public extension NSAttributedString {
    
    static func cm_attributes(font: UIFont, color: UIColor? = nil, paragraph style: NSMutableParagraphStyle? = nil, lineStyle: CMLineStyle = .normal) -> [NSAttributedString.Key : Any] {
        
        var paragraphStyle = style
        if paragraphStyle == nil {
            paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle?.lineBreakMode = .byTruncatingTail
            paragraphStyle?.alignment = .left
        }
        
        var aColor = color
        if aColor == nil { aColor = .black }
        
        var attrs: [NSAttributedString.Key: Any] = [.font: font]
        attrs[.paragraphStyle] = paragraphStyle
        attrs[.foregroundColor] = color
        
        if lineStyle == .underline {
            attrs[.strikethroughStyle] = 0
            attrs[.underlineStyle] = 1
        }
        else if lineStyle == .strikethrough {
            attrs[.strikethroughStyle] = 1
            attrs[.underlineStyle] = 0
        }
        return attrs
    }
}

// string 宽高
public extension NSAttributedString {
    
    /*
     * 计算富文本Size, 推荐
     */
    func cm_stringSize(maxSize: CGSize) -> CGSize {
#if canImport(UIKit)
        let option: NSStringDrawingOptions = [
            .usesLineFragmentOrigin,
             .usesFontLeading
        ]
#endif
        
#if canImport(AppKit)
        let option: NSString.DrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
#endif
        
        let rect: CGRect = boundingRect(with: maxSize, options: option, context: nil)
        return CGSize(width: rect.width + 1, height: rect.height + 1)
    }
    
    func cm_stringHeight(maxWidth: CGFloat) -> CGFloat {
        let maxSize = CGSize(width: maxWidth, height: CGFloat(MAXFLOAT))
        return cm_stringSize(maxSize: maxSize).height
    }
    
    var cm_stringOneLineHeight: CGFloat {
        return cm_stringHeight(maxWidth: CGFloat(MAXFLOAT))
    }
    
    /** 计算宽度 +1 为了容错 */
    func cm_stringWidth(maxHeight: CGFloat) -> CGFloat {
        let maxSize = CGSize(width: CGFloat(MAXFLOAT), height: maxHeight)
        return cm_stringSize(maxSize: maxSize).width + 1
    }
    
    var cm_stringOneLineWidth: CGFloat {
        return cm_stringWidth(maxHeight: CGFloat(MAXFLOAT))
    }
    // 同上
    var cm_stringOneLineWidth1: CGFloat {
        return cm_stringWidth(maxHeight: 1)
    }
}

/*
 * 计算富文本Size, 不推荐, 建议使用 cm_stringSize:
 */
public extension NSAttributedString {
    
    func st_stringSize(maxSize: CGSize) -> CGSize {
        let framesetter = CTFramesetterCreateWithAttributedString(self)
        let size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, .zero, nil, maxSize, nil)
        return CGSize.init(width: size.width + 1, height: size.height + 1)
    }
    
    func st_stringHeight(maxWidth: CGFloat) -> CGFloat {
        let maxSize = CGSize(width: maxWidth, height: CGFloat(MAXFLOAT))
        return cm_stringSize(maxSize: maxSize).height
    }
    
    var st_stringOneLineHeight: CGFloat {
        return cm_stringHeight(maxWidth: CGFloat(MAXFLOAT))
    }
    
    /** 计算宽度 +1 为了容错 */
    func st_stringWidth(maxHeight: CGFloat) -> CGFloat {
        let maxSize = CGSize(width: CGFloat(MAXFLOAT), height: maxHeight)
        return cm_stringSize(maxSize: maxSize).width + 1
    }
    
    var st_stringOneLineWidth: CGFloat {
        return cm_stringWidth(maxHeight: CGFloat(MAXFLOAT))
    }
    // 同上
    var st_stringOneLineWidth1: CGFloat {
        return cm_stringWidth(maxHeight: 1)
    }
}
